<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="请输入部门名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="queryDepts">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd()">新增</el-button>
        </el-form-item>
          <!--抽屉-->
        <el-form-item>
          <el-button @click="drawer = true" type="primary" style="margin-left: 16px;">点我打开抽屉，也可双击数字字典</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--抽屉-->
    <el-drawer
      size="50%"
      title="字典明细"
      :visible.sync="drawer"
      :direction="direction"
      :before-close="handleClose">
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true">
          <el-form-item>
            <el-button type="primary" @click="systemAdd()">新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <span>
    <!--抽屉列表 -->
      <el-table :data="gridData">
      <el-table-column type="selection" width="60">
      </el-table-column>
      <el-table-column type="index" width="75">
      </el-table-column>
      <el-table-column property="name" label="明细名" width="200"></el-table-column>

        <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="systemsave(scope.$index, scope.row,'修改')">编辑</el-button>
          <el-button type="danger" size="small" @click="systemDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
      </span>
      <el-col :span="24" class="toolbar">
        <!--  分页      -->
        <el-pagination
          @size-change="handleSizeChange1"
          @current-change="handleCurrentChange1"
          :current-page="currentPage1"
          :page-sizes="[1,5, 10, 15, 30]"
          :page-size="pageSize1"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total1"
          style="float:right;">
        </el-pagination>
      </el-col>
    </el-drawer>



    <!--列表-->
    <el-table :data="depts" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="60">
      </el-table-column>
      <el-table-column type="index" width="75">
      </el-table-column>
      <el-table-column prop="sn" label="编号" width="120" sortable>
      </el-table-column>
      <el-table-column label="数据字典" width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <el placement="top">
            <div slot="reference" class="name-wrapper">
<!-- 这里调用show方法把当前的数据带走,用来获取id的-->
              <span @dblclick="show(scope.row)">{{ scope.row.name }}</span>
            </div>
          </el>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handlesave(scope.$index, scope.row,'修改')">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>

<!--  分页      -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="float:right;">
      </el-pagination>

    </el-col>

    <!--添加.编辑.保存界面-->
    <!--
      :visible.sync 控制模态框是否显示
      :rules="saveFormRules"  表单验证的绑定
      prop="sn" 专门用来做表单验证

    -->
    <el-dialog :title="saveTitle" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="编号" prop="sn">
          <el-input v-model="saveForm.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="字典名称" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>

      <!-- 用于抽屉添加修改   -->
    <el-dialog :title="saveTitle" :visible.sync="systemFormVisible" :close-on-click-modal="false">
      <el-form :model="systemForm" label-width="80px" :rules="systemFormRules" ref="systemForm">
        <el-form-item label="明细" prop="name">
          <el-input v-model="systemForm.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="systemFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="systemSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
export default {
  data() {
    return {
      //控制抽屉是否显示
      drawer: false,
      //控制抽屉弹出的方向
      direction: 'rtl',
      filters: { //高级查询
        keyword: ''
      },
      depts: [],
      gridData:[],//抽屉列表
      total: 0,
      total1: 0,
      currentPage:1,
      currentPage1:1,
      pageSize:10,
      pageSize1:10,
      listLoading: false,

      //批量删除使用的
      sels: [],//列表选中列

      //新增，或者修改用
      saveFormVisible: false,//新增界面是否显示
      systemFormVisible: false,//抽屉新增界面是否显示
      saveTitle:"",//设置标题  新增或者是编辑
      saveLoading: false,//忙等
      saveFormRules: { //表单的验证
        sn: [
          //   必传         提示信息                失去焦点触发
          { required: true, message: '编号', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '字典名字', trigger: 'blur' }
        ]
      },
      //抽屉表单验证
      systemFormRules: { //表单的验证
        name: [
          { required: true, message: '明细名字', trigger: 'blur' }
        ]
      },

      employees:[],//存放员工列表的数据
      tree:[],  //用来存放部门树
      //编辑和新增界面数据  弹出框的form标单
      saveForm: {
        id: null,
        sn: '',
        name: '',
      },
      //编辑和新增界面数据  弹出框的form标单
      systemForm: {
        id: null,
        name: '',
        types_id:null,
      },
    }
  },
  methods: {
    //控制抽屉关闭的方法
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    //当前页的事件
    handleCurrentChange(val) { //自动获取当前页码
      this.currentPage = val;
      this.getdepts(); //刷新列表
    },
    //选择条数的事件
    handleSizeChange(val){//自动获取当前页的条数
      this.pageSize=val
      this.getdepts();
    },
    handleCurrentChange1(val) { //自动获取当前页码
      this.currentPage1 = val;
      this.getmingxi(); //刷新列表
    },
    //选择条数的事件
    handleSizeChange1(val){//自动获取当前页的条数
      this.pageSize1=val
      this.getmingxi();
    },
    queryDepts(){
      this.currentPage = 1
      this.getdepts()
    },
    //获取用户列表
    getdepts() {  //这里是发送请求
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.filters.keyword,
        sn: this.filters.sn,
        state: this.filters.state,
      };
      this.listLoading = true;//开启忙等
      //获取部门的分页参数
      this.$http.post("/system/pageList",para).then(res=>{
        this.depts=res.data.data
        this.total=res.data.total
        this.listLoading = false; //加载完就关闭忙等
      })
    },
    show(row){
      this.currentPage1 = 1
      this.pageSize1 = 5
      this.tid = row.id
      this.getmingxi()
      this.drawer = true
    },
    //抽屉获取用户列表
    getmingxi() {  //这里是发送请求
      let para = {
        currentPage: this.currentPage1,
        pageSize: this.pageSize1,
        keyword: this.filters.keyword,
        types_id: this.tid,
      };
      this.listLoading = true;//开启忙等
      //获取抽屉的分页参数
      this.$http.post("/systems/pageList",para).then(res=>{
        this.gridData=res.data.data
        this.total1=res.data.total
        this.listLoading = false; //加载完就关闭忙等
      })
    },

    //抽屉删除
    systemDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '温馨提示', {
        type: 'warning'  //设置图标类型
      }).then(() => { //这是点了确定要做的事
        this.listLoading = true; //开启忙等
        //发送请求
        //"/dept/"+row.id 因为后端使用的{id} 所以用row 把后端的数据传的到前端
        this.$http.delete("/systems/"+row.id).then(res=>{
          this.listLoading = false; //数据加载出来了，就关闭忙等
          //做判断，是否删除成功 跟批量删除是一样的
          //如果是成功的就执行成功弹框 ,然后并刷新列表
          if(res.data.success){
            this.$message({
              message: res.data.msg,
              type: 'success'
            })
            this.currentPage1 = 1 //删除后回到第一页
            this.getmingxi() //删除成功一定要刷新列表
          }else { //是失败执行失败弹框
            this.$message({
              message: res.data.msg,
              type: 'error'
            })
          }
        })
      }).catch(() => {//取消要做的事情
      });
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '温馨提示', {
        type: 'warning'  //设置图标类型
      }).then(() => { //这是点了确定要做的事
        this.listLoading = true; //开启忙等
        //发送请求
        //"/dept/"+row.id 因为后端使用的{id} 所以用row 把后端的数据传的到前端
        this.$http.delete("/system/"+row.id).then(res=>{
          this.listLoading = false; //数据加载出来了，就关闭忙等
          //做判断，是否删除成功 跟批量删除是一样的
          //如果是成功的就执行成功弹框 ,然后并刷新列表
          if(res.data.success){
            this.$message({
              message: res.data.msg,
              type: 'success'
            })
            this.currentPage = 1 //删除后回到第一页
            this.getdepts(); //删除成功一定要刷新列表
          }else { //是失败执行失败弹框
            this.$message({
              message: res.data.msg,
              type: 'error'
            })
          }
        })
      }).catch(() => {//取消要做的事情
      });
    },

    //显示编辑界面
    handlesave: function (index, row,title) {
      if(this.$refs['editForm']){
        this.$refs['editForm'].resetFields();//重置表单验证
      }
      this.saveTitle=title
      this.saveFormVisible = true; //是否显示
      this.saveForm = Object.assign({}, row); //给表单赋值
    },
    //抽屉显示编辑界面
    systemsave: function (index, row,title) {
      if(this.$refs['editForm']){
        this.$refs['editForm'].resetFields();//重置表单验证
      }
      this.saveTitle=title
      this.systemFormVisible = true; //是否显示
      this.systemForm = Object.assign({}, row); //给表单赋值
    },
    //显示新增界面
    handleAdd: function () {
      this.saveTitle= "新增"
      this.saveForm = {
        id: null,
        sn:"",
        name: '',
      }
      this.saveFormVisible = true;
    },
    //抽屉显示显示新增界面
    systemAdd: function () {
      this.saveTitle= "新增"
      this.systemForm = {
        id: null,
        name: '',
        types_id: '',
      }
      this.systemFormVisible = true;
    },
    //编辑
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => { //手动触发验证规则
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.saveLoading = true; //开启忙等
            //因为双向绑定
            let para = Object.assign({}, this.saveForm); //克隆
            //解决修改上级部门
            if(para.parent_id){
              if (typeof  para.parent_id =="object"){
                para.parent_id = para.parent_id[para.parent_id.length-1];
              }
            }


            //发送请求
            this.$http.put("/system",para).then(res=>{
              //判断，如果成功的话就弹出提示框
              if(res.data.success){
                this.saveLoading = false; //成功就关闭忙等
                this.$message({
                  message: res.data.msg,
                  type: 'success'
                });
                //重置表单验证
                this.$refs['saveForm'].resetFields();
                this.saveFormVisible = false;
                this.getdepts();
              }else {
                this.$message({
                  message: res.data.msg,
                  type: 'error'
                })
                this.saveLoading = false;
              }
            })

            });
        }
      });
    },
    //抽屉编辑
    systemSubmit: function () {  //validate
      this.$refs.systemForm.validate((valid) => { //手动触发验证规则
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.saveLoading = true; //开启忙等
            //因为双向绑定
            let para = Object.assign({},this.systemForm); //克隆
            console.log(para)
              para.types_id = this.tid
            console.log(para.types_id)
            //发送请求
            this.$http.put("/systems",para).then(res=>{
              //判断，如果成功的话就弹出提示框
              if(res.data.success){
                this.saveLoading = false; //成功就关闭忙等
                this.$message({
                  message: res.data.msg,
                  type: 'success'
                });
                //重置表单验证
                this.$refs['systemForm'].resetFields();
                this.systemFormVisible = false;
                this.getmingxi();
              }else {
                this.$message({
                  message: res.data.msg,
                  type: 'error'
                })
                this.saveLoading = false;
              }
            })

          });
        }
      });
    },

    selsChange: function (sels) {//给多选框绑定事件，如果勾选就会获取当前的值
      this.sels = sels; //将选择器中的数据赋值给this.sels
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);  //返回一个ids数组，千万不要加tostring 不然返回的是字符串
      console.log(ids)
      this.$confirm('确认删除选中记录吗？', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;  //开启忙等
        console.log(ids)
        //NProgress.start();
        this.$http.patch("/system/del",ids).then(res=>{
          this.listLoading = false; //如果加载进去就关闭忙等
          //做判断，如果他成功了，就弹出提示框删除成功，或者就是失败
          if (res.data.success){
            //成功就弹出提示框 并刷新列表
            this.$message({
              message: res.data.msg,
              type: 'success'
            });
            this.getdepts();//刷新列表
          }else {  //失败也弹出提示框 提醒删除失败 删除失败就不需要刷新列表，保持原数据
            this.$message({
              message: res.data.msg,
              type: 'error'
            })
          }
        })
      }).catch(() => { //这是点取消后做的事，点取消现在不需要做任何事

      });
    }
  },
  mounted() {
    this.getdepts();
    this.getmingxi();
  }
}

</script>

<style scoped>

</style>
